package com.flower.consumer;

import com.flower.config.RedisSupport;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author shanks on 2019/5/16
 */
@Component
@Order(value = 1)
@Slf4j
public class StartLoader implements ApplicationRunner {


    @Autowired
    private RedisSupport redisSupport;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("#######################################系统初始化#######################################");
        loadSysParams();
        System.out.println("#######################################初始化完成#######################################");
    }

    public void loadSysParams() throws InterruptedException {
        System.out.println("【系统参数】加载中...");
        while (true) {
            long currTime = System.currentTimeMillis() / 1000;
            String message = redisSupport.zrangeRemoveByScore("v1", currTime);
            if (StringUtil.isNullOrEmpty(message)) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                log.info("消息被消费：" + message);
            }
        }
    }

}
